﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Text.RegularExpressions;
using System.Configuration;

namespace NetManager_Server
{
    public partial class FormNguoiDung : Form
    {
        public FormNguoiDung()
        {
            InitializeComponent();
        }
        function fn = new function();
        ket_noi kn = new ket_noi();
        protected DataSet dts;
        private DataTable nhanvien;

        private void doc_nhanvien()
        {
            string lenh = "select manv, hoten from nhanvien where manv not in (select manv from dangnhap)";
            nhanvien = function.Doc_bang(lenh);
        }

        private void doc_manv()
        {
            manv.DisplayMember = "manv";
            manv.ValueMember = "manv";
            manv.DataSource = nhanvien;
        }

        private void doc_hoten()
        {
            hoten.DisplayMember = "hoten";
            hoten.ValueMember = "hoten";
            hoten.DataSource = nhanvien;
        }
        private void btthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void hoten_DropDown(object sender, EventArgs e)
        {
            doc_nhanvien();
            doc_manv();
            doc_hoten();
        }

        private string ghepchuoi()
        {
            string chuoi = hoten.Text;
            string[] chuoi_cat = chuoi.Split(' ');
            string chuoiemail = "";
            int dai = chuoi_cat.Length;
            for (int i = 0; i <= dai - 2; i++)
            {
                chuoiemail += chuoi_cat[i].Substring(0, 1).ToLower();
            }
            return chuoiemail += chuoi_cat[dai - 1].ToLower();
        }

        private void hoten_TextChanged(object sender, EventArgs e)
        {
            if (hoten.Text != "")
            {
                this.lbhoten.Text = "";
            }
            taikhoan.Text = ghepchuoi();
        }

        private void taikhoan_TextChanged(object sender, EventArgs e)
        {
            if (taikhoan.Text != "")
            {
                this.lbtaikhoan.Text = "";
            }
        }

        private void matkhau_TextChanged(object sender, EventArgs e)
        {
            if (matkhau.Text != "")
            {
                this.lbmatkhau.Text = "";
            }
        }

        private void quyen_TextChanged(object sender, EventArgs e)
        {
            if (quyen.Text != "Chọn")
            {
                this.lbquyen.Text = "";
            }
        }

        private void btthem_Click(object sender, EventArgs e)
        {
            if (hoten.Text == "")
            {
                this.lbhoten.Text = "Vui lòng chọn nhân viên !";
                hoten.Focus();
            }
            else if (taikhoan.Text == "")
            {
                this.lbtaikhoan.Text = "Vui lòng nhập tài khoản !";
                taikhoan.Focus();
            }
            else if (matkhau.Text == "")
            {
                this.lbmatkhau.Text = "Vui lòng nhập mật khẩu !";
                matkhau.Focus();
            }
            else if (quyen.Text == "Chọn")
            {
                this.lbquyen.Text = "Vui lòng chọn quyền người dùng !";
                quyen.Focus();
            }
            else
            {
                dts = fn.AddNguoiDung(manv.Text, taikhoan.Text, matkhau.Text, quyen.Text);
                view.DataSource = dts.Tables[0];
                hoten.Text = "";
                taikhoan.Text = "";
                matkhau.Text = "";
            }
        }

        private void FormNguoiDung_Load(object sender, EventArgs e)
        {
            dts = fn.LoadData("DANGNHAP");
            view.DataSource = dts.Tables[0];
        }

        private void btxoa_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("Bạn có chắc chắn xóa dòng đã chọn không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    IEnumerator ie = view.SelectedRows.GetEnumerator();
                    while (ie.MoveNext())
                    {
                        DataGridViewRow row = (DataGridViewRow)ie.Current;
                        view.Rows.Remove(row);
                    }
                }
            }
            catch (Exception)
            {

            }
            fn.SaveDataDelete(dts, "DANGNHAP");
        }

        private void hoten_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }
    }
}
